package com.cloud.local.lock.controller;

import com.cloud.local.lock.annotation.LocalLock;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author zhaoyangfu - 2020/7/22 15:35
 */
@RestController
@RequestMapping("/books")
public class BookController {

    /**
     * 由于这里不能如@Cacheable一样使用SPEL,所以是通过aop中手动解析参数来生成key.
     * 有需求也可以模仿@Cacheable的原理来支持SPEL.
     */
    @GetMapping
    @LocalLock(key = "book:arg[0]")
    public String query(@RequestParam String token) throws InterruptedException {
        TimeUnit.SECONDS.sleep(4);
        return "success => " + token;
    }
}
